package com.jkdroid.mayademo;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.jkdroid.maya.TestClass;
import com.jkdroid.maya.helper.DraftHelper;
import com.jkdroid.maya.helper.MultiClickHelper;
import com.jkdroid.maya.helper.SoftKeyboardStateHelper;
import com.jkdroid.maya.util.ToastUtils;
import com.jkdroid.maya.view.FlowView;

public class MainActivity extends Activity implements View.OnClickListener, View.OnLongClickListener {

    MultiClickHelper multiClickHelper;
    DraftHelper draftHelper;
    FlowView fv;
    boolean needShow = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final EditText mEt = (EditText) findViewById(R.id.et);
        findViewById(R.id.btn).setOnClickListener(this);
        findViewById(R.id.btn).setOnLongClickListener(this);
        draftHelper = new DraftHelper(this.getApplicationContext(), "one", 60 * 1000 * 10);
        draftHelper.setEditTexts(mEt);
        mEt.setText(draftHelper.getText(mEt, "is null"));
        Toast.makeText(getApplicationContext(), TestClass.getTestString(), Toast.LENGTH_SHORT).show();
        multiClickHelper = new MultiClickHelper(MultiClickHelper.DOUBLE_CLICK);
        new SoftKeyboardStateHelper(this).addSoftKeyboardStateListener(new SoftKeyboardStateHelper.SoftKeyboardStateListener() {
            @Override
            public void onSoftKeyboardOpened(int keyboardHeightInPx) {
                ToastUtils.showImmediately(MainActivity.this, "打开键盘:" + keyboardHeightInPx);
            }

            @Override
            public void onSoftKeyboardClosed() {
                ToastUtils.showImmediately(MainActivity.this, "收起键盘");
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (needShow && fv != null && !fv.isShowing()) {
            fv.show();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (!needShow && fv != null && fv.isShowing()) {
            fv.hide();
        }
    }

    @Override
    public void onBackPressed() {
        if (multiClickHelper.onClick()) {
            draftHelper.saveAll();
            finish();
        }
    }

    @Override
    public void onClick(View v) {
        if (fv == null){
            fv = FlowView.get(this)
                    .setView(R.layout.flowview)
                    .setAlpha(0.5f)
                    .setOnBorderedListener(new FlowView.OnBorderedListener() {
                        @Override
                        public void onEdge(boolean changed, int border, int action) {
                            Log.i("onEdge", changed+","+border+","+action);
                            if (changed){
                                Object tag = fv.getView().getTag();
                                if (tag == null || !(tag instanceof TextView)){
                                    TextView v = (TextView) fv.getView().findViewById(R.id.tv);
                                    fv.getView().setTag(v);
                                }
                                TextView tv = (TextView) fv.getView().getTag();
                                String txt = "";
                                if ((FlowView.BORDER_LEFT & border) > 0){
                                    txt += "(LEFT)";
                                }
                                if ((FlowView.BORDER_RIGHT & border) > 0){
                                    txt += "(RIGHT)";
                                }
                                if ((FlowView.BORDER_TOP & border) > 0){
                                    txt += "(TOP)";
                                }
                                if ((FlowView.BORDER_BOTTOM & border) > 0){
                                    txt += "(BOTTOM)";
                                    fv.setAlpha(1.0f).requestRefresh();
                                    fv.setDrawable(false);
                                }
                                tv.setText(TextUtils.isEmpty(txt) ? "悬浮框" : txt);
                                Log.i("弹弹弹"+action, txt);
                            }
                        }
                    })
                    .setFlingType(FlowView.FLING_TYPE_STOP_ON_EDGE)
                    .setGravity(Gravity.CENTER, 0, 0)
//                    .setPosition(300,300)
//                    .setAnimations(0)
                    .build();
        }
        fv.toggle();
        needShow = fv.isShowing();
    }

    @Override
    public boolean onLongClick(View v) {
        if (fv != null){
            fv.setFlingType(fv.getFlingType() == FlowView.FLING_TYPE_BOUNCE ? FlowView.FLING_TYPE_STOP_ON_EDGE : FlowView.FLING_TYPE_BOUNCE);
            TextView tv = (TextView) v;
            tv.setText(fv.getFlingType() == FlowView.FLING_TYPE_STOP_ON_EDGE ? "Edge" : "Bounce");
            fv.setDrawable(true);
        }
        return true;
    }
}
